#include "PEIncludes.h"

#include "PELog.h"
#include "PETools.h"

namespace pe
{

	int PELogOpen()
	{
		g_LogPath = new wchar_t[_MAX_PATH];
		size_t path_len;
		GetPatternedPath(g_LogPath, &path_len, "%workingdir%%name%%ext%", L"/errors.log");

		g_Log = _wfopen(g_LogPath, L"w+");
		fclose(g_Log);

		return PE_OK;
	}

	int PELogWrite(const char* a_Message, ...)
	{
		wchar_t* msg;
		PE_STRTOWIDE(msg, a_Message);

		wchar_t buffer[1024];

		va_list args;
		va_start(args, a_Message);
		vswprintf(buffer, msg, args);
		va_end(args);

		g_Log = _wfopen(g_LogPath, L"a");
		printf("%S", buffer);
		fwprintf(g_Log, buffer);
		fclose(g_Log);

		delete msg;

		return PE_OK;
	}

};